package com.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dao.IUserDao;
import com.dao.UserDaoImpl;
import com.entity.User;

/**
 * Servlet implementation class ModifyInfoServlet
 */
public class ModifyInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private IUserDao userDao  = new UserDaoImpl();    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ModifyInfoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession();
		User user=(User)session.getAttribute("user");
		
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		String phone = request.getParameter("phone");
		String address = request.getParameter("address");
	
		if(name!=null) {
			user.setName(name);
		}
		if(password!=null){
			user.setPassword(password);
		}
		if(phone!=null){
			user.setPhone(phone);
		}
		if(address!=null){
			user.setAddress(address);
		}
		userDao.updateUser(user);
		System.out.println("WOW");
		request.getRequestDispatcher("/personalinfo.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession();
		User user=(User)session.getAttribute("user");
		
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		String phone = request.getParameter("phone");
		String address = request.getParameter("address");
		
	
		if(name!=null) {
			user.setName(name);
		}
		if(password!=null){
			user.setPassword(password);
		}
		if(phone!=null){
			user.setPhone(phone);
		}
		if(address!=null){
			user.setAddress(address);
		}
		userDao.updateUser(user);
	
		request.getRequestDispatcher("/personalinfo.jsp").forward(request, response);
	}

}
